package cn.donting.jme.expansion.attribute;


import cn.donting.jme.EditorContent;
import cn.donting.jme.event.scene.SceneChangeEvent;

import java.awt.*;

/**
 * InspectTab 检查属性，用于各种类型的 输入
 *
 * @param <T>
 */
public abstract class Attribute<T> {
    private ValueChangeListener<T> valueChangeListener;
    /**
     * valueChange 是否在 jme 线程执行
     */
    boolean jme;
    private String key;
    private Object value;
    private boolean sceneChangeEvent = true;

    public Attribute() {
    }

    public void setSceneChangeEvent(boolean sceneChangeEvent) {
        this.sceneChangeEvent = sceneChangeEvent;
    }

    public abstract void setValue(T value);

    public abstract T getValue();

    public abstract void setTitle(String title);

    public abstract String getTitle();

    public abstract Container getUIContainer();

    public void valueChange(T newV) {
        if (newV == value) {
            return;
        }
        if (value != null && value.equals(newV)) {
            return;
        }
        if (value != null && valueChangeListener != null) {
            EditorContent.runSwing(() -> {
                EditorContent.postEvent(new SceneChangeEvent(this));
            });
        }
        value = newV;
        if (jme) {
            EditorContent.runJme(() -> {
                if (this.valueChangeListener != null) {
                    this.valueChangeListener.change(newV);
                }
            });
        } else {
            EditorContent.runSwing(() -> {
                if (this.valueChangeListener != null) {
                    this.valueChangeListener.change(newV);
                }
            });
        }
    }

    public abstract void setTip(String tip);

    /**
     * 在 jme 中执行
     *
     * @param valueChangeListener
     */
    public void setValueChangeListener(ValueChangeListener<T> valueChangeListener) {
        this.setValueChangeListener(true, valueChangeListener);
    }

    public void setValueChangeListener(boolean jme, ValueChangeListener<T> valueChangeListener) {
        this.valueChangeListener = valueChangeListener;
        this.jme = jme;
    }

    public abstract void setEnable(boolean enable);

    public abstract void showEnableCheckBox(boolean enable);

    @Override
    public String toString() {
        return getTitle() + "->" + value.toString();
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }
}
